package day02;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author Jetwang
 * @version 1.0
 * @since 2023/3/12 10:34
 * 从上到下打印二叉树
 */
public class Offer31 {
    public int[] levelOrder(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        LinkedList<TreeNode> queue = new LinkedList<>();
        if (root == null) {
            return new int[0];
        }
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode p = queue.poll();
            list.add(p.val);
            if (p.left != null) {
                queue.offer(p.left);
            }
            if (p.right != null) {
                queue.offer(p.right);
            }
        }
        int[] arr = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            arr[i] = list.get(i);
        }
        return arr;
    }
}
